package com.ericsson.lgc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
//@WebServlet("/HelloServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }
//	/**
//	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
//	 */
//	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		// TODO Auto-generated method stub 
//		String name=request.getParameter("name");
//		System.out.println(name);
//		response.sendRedirect("index.html");
//	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse request)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpServletRequest httpRequest=(HttpServletRequest)request;
		
		 request.setCharacterEncoding("UTF-8"); //Post时，与页面前端charset一致即可。
		 String name=request.getParameter("name");
		 String pwd=request.getParameter("pwd");
		 if ("zs".equals(name) && "123".equals(pwd)){
			 request.getSession().setAttribute("user", name);
			 //response.sendRedirect("hello");
			 response.sendRedirect(httpRequest.getContextPath()+"/lgc/main.jsp");
		 }else{
			 String errInfo="";
			 if ("zs".equals(name)){
				 if (!"123".equals(pwd)){	 
					 errInfo="wrongPassword";
				 }
			 }else{
				 errInfo="wrongName";
			 }

			 
			 response.sendRedirect(httpRequest.getContextPath()+"/lgc/index.jsp?errInfo="+errInfo);
			 //request.getRequestDispatcher("/login.html?errInfo="+errInfo).forward(request, response);
		 }
		 
	}


}
